package com.mycompany.dewcineplanet.repository;

import java.util.ArrayList;
import java.util.List;

import com.mycompany.dewcineplanet.model.Programacion;
import com.mycompany.dewcineplanet.model.Reserva;
import com.mycompany.dewcineplanet.model.Sala;

public class ReservaRepository {

	List<Reserva> list = null;

	public ReservaRepository(){
		list = new ArrayList<Reserva>();
		list.add(new Reserva(new Long(1), new Long(1), new Long(1)));
		list.add(new Reserva(new Long(2), new Long(1), new Long(2)));
		list.add(new Reserva(new Long(3), new Long(1), new Long(3)));
	}

	public List<Reserva> obtenerTodoReserva(){
		return list;
	}

	public boolean agregarReserva(Long programacion, Long cliente){
		boolean flag = false;
		ProgramacionRepository programacionRepository = new ProgramacionRepository();
		SalaRepository salaRepository = new SalaRepository();
		Programacion _programacion = programacionRepository.obtenerProgramacion(programacion);
		Sala sala = salaRepository.obtenerSala(_programacion.getSala());
		List<Reserva> _list = obtenerReservaProgramacion(_programacion.getCodigo());
		if(sala.getCapacidad().longValue() > _list.size()){
			list.add(new Reserva(new Long(list.size() + 1), programacion, cliente));
			flag = true;
		}
		return flag;
	}

	public List<Reserva> obtenerReservaProgramacion(Long programacion){
		List<Reserva> result = null;
		if(list != null && list.size() > 0){
			for(int i = 0; i < list.size(); i++){
				Reserva reserva= list.get(i);
				if(reserva.getProgramacion().equals(programacion)){
					if(result == null) result = new ArrayList<Reserva>();
					result.add(reserva);
				}
			}
		}
		return result;
	}
}